package com.cloud.config.web;

import com.cloud.common.exception.GlobalExceptionResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvc配置
 *
 * @author wangjian
 * @version 1.0.0
 * @create 2020年10月24日 16:43
 * @since JDK 1.8
 * Life is so short,do something to make yourself happy,such as coding
 */
@Slf4j
@Configuration
@Import({GlobalExceptionResolver.class})
public class CloudNativeWebConfig implements WebMvcConfigurer {

    /**
     * 资源处理器
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html", "doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    /**
     * 跨域
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                // 1 设置访问源地址
                .allowedOrigins("*")
                // 2 设置访问源请求头
                .allowedHeaders("*")
                // 3 设置访问源请求方法
                .allowedMethods("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS")
                .maxAge(60 * 60 * 24 * 7);
    }

}
